import { Component, OnInit, Injector, TemplateRef, ViewChild, Input } from '@angular/core';
import { NgZorroPageBaseComponent } from '../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component';
import { HttpService } from "../../core/service/http.service";
import { Router, NavigationExtras } from "@angular/router";
import {
  AndOr, Compare, ExpressionPair, PagedResultDto, PagerSearchInput, ElementViewType, BaseComponentConfig,
  SystemFormControl, FieldType, ElementShowType, CustomNavigation, ClassInfoOutput, NavigatType
} from "../..";
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import {
  FormBuilder, FormGroup, FormsModule, ReactiveFormsModule, Validators,
  FormControl
} from "@angular/forms";
import { CourseInfoOutput } from '../../core/model/CourseInfoDto';
import { CourseClassComponent } from './course-class.component';
import { CourseStudentComponent } from './course-student.component';
import { CourseSubjectComponent } from './course-subject.component';
import { Type } from '@angular/compiler/src/core';


@Component({
  selector: 'app-course-component',
  templateUrl: '../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component.html',
  styleUrls: ['../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component.scss']
})
export class CourseComponent extends NgZorroPageBaseComponent implements OnInit {

  constructor(injector: Injector,
    public http: HttpService) {
    super(injector,
      new BaseComponentConfig("TeacherInfo/GetCoursePager", "TeacherInfo/CreateCourse", "TeacherInfo/UpdateCourse",
        "TeacherInfo/GetCourse", "TeacherInfo/DeleteCourse", "id", "Course", "课程管理",
        new Array<CustomNavigation>(
          new CustomNavigation('班级管理', '', ElementShowType.TableOperation, "anticon anticon-home", NavigatType.Model, 'class'),
          new CustomNavigation('学生管理', '', ElementShowType.TableOperation, 'anticon anticon-user', NavigatType.Model, 'student'),
          new CustomNavigation('演练管理', '', ElementShowType.TableOperation, "anticon anticon-appstore", NavigatType.Model, 'subject'),
          new CustomNavigation('开启/结束授课', '', ElementShowType.TableOperation, "anticon anticon-play-circle-o", NavigatType.Model, 'start'),
          new CustomNavigation(
            '课程打分', '/app/courseScore', ElementShowType.TableOperation, 'anticon anticon-bars', NavigatType.Route, 'courseScore'),
        )
      ));
  }

  getNewEntity(): CourseInfoOutput {
    return new CourseInfoOutput();
  }
  async navigateCustom(url: string, pid: any) {
    this.router.navigate([url, pid]);
    // this.router.navigate(['app/mydrill/editdrill',40]);
  }
  async showModalForComponent(entity: CourseInfoOutput, tag: any) {
    const keyValue = entity.id;
    let compoentn: any = null;
    switch (tag) {
      case 'class':
        compoentn = CourseClassComponent;
        break;
      case 'student':
        compoentn = CourseStudentComponent;
        break;
      case 'subject':
        compoentn = CourseSubjectComponent;
        break;
    }
    if ( tag === 'start' ) {
      const issatrted = await this.http.get<any>(`Course/CourseIsStarted?CourseId=${keyValue}`);
      if (issatrted) {
        this.nzModalService.confirm({
          nzTitle: "是否结束课程授课",
          nzContent: '<p>点击确定后，可关闭当前课程</p>',
           nzOnOk:  () => {
  
            const req =  this.http.get<any>(`Course/EndTaught?CourseId=${keyValue}`);
          }
        });
      } else {
        this.nzModalService.confirm({
          nzTitle: "是否开启课程授课",
          nzContent: '<p>开启后，可开启新的演练</p>',
           nzOnOk:  () => {
  
            const req =  this.http.get<any>(`Course/StartTaught?CourseId=${keyValue}`);
          }
        });
      }

    } else {
      const subscription = this.nzModalService.create({
        nzTitle: `${entity.courseName}`,
        nzContent: compoentn,
        nzComponentParams: {
          pKeyFieldValue: keyValue,
          caption: entity.courseName
        },
        nzFooter: null,
        nzWidth: document.documentElement.clientWidth - 60,
      });
    }
  }
}